import { listCategoryDict, listCategoryTree } from '@/apis'
import type { LabelValueState } from '@/types/global'
import type { TreeNodeData } from '@arco-design/web-vue'
import { ref } from 'vue'

/** 分类模块树 */
export function useCategory(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const categoryList = ref<TreeNodeData[]>([])

  const getCategoryList = async (name?: string) => {
    try {
      loading.value = true
      const res = await listCategoryTree({ description: name })
      categoryList.value = res.data
      options?.onSuccess && options.onSuccess()
    } finally {
      loading.value = false
    }
  }
  return { categoryList, getCategoryList, loading }
}

/** 分类模块字典 */
export function useCategoryDict(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const categoryDicList = ref<LabelValueState[]>([])

  const getCategoryDicList = async () => {
    try {
      loading.value = true
      const res = await listCategoryDict({ parentId: 0 })
      categoryDicList.value = res.data
      options?.onSuccess && options.onSuccess()
    } finally {
      loading.value = false
    }
  }
  return { categoryDicList, getCategoryDicList, loading }
}
